﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nancy;
using nwrole.Module.lib;
using nwrole.toolbox.sync.client;
using nwrole.toolbox.sync.entity;

namespace nwrole.toolbox.sync.ConfigModule.Modules
{
    public class HomeModule : NancyModule
    {
        private static int SyncInstanceId = 1; // Temp hardcoded, should be specified from a dropdown on the website

        public HomeModule()
        {
            Get["/"] = parameters =>
            {
                SyncSourceServiceClient client = GetSourceServiceClient();
                if (client.GetDeployStatus() == 0)
                    return Response.AsRedirect("/status");

                string instanceStr = Request.Query.instance;

                int instanceId = SyncInstanceId;
                if (!string.IsNullOrEmpty(instanceStr))
                    instanceId = int.Parse(instanceStr);

                var list = client.GetEntities(instanceId);

                if (list == null)
                    list = new List<SyncEntity>(); // Create an empty list for the view's sake

                ViewBag.InstanceId = instanceId;

                return View["Index", list];
            };

            Get["/status"] = parameters => View["status", 5];

            Get["/DeploySourceEnvironment"] = parameters =>
            {
                SyncSourceServiceClient client = GetSourceServiceClient();
                client.DeploySourceEnvironment();

                //return Response.AsJson(new { newValue = 1, success = true });

                return Response.AsRedirect("/status");
            };

            Get["/DeployDestEnvironment"] = parameters =>
            {
                SyncDestinationServiceClient destClient = GetDestinationServiceClient();
                destClient.DeployDestEnvironment();

                //return Response.AsJson(new { newValue = 1, success = true });
                return Response.AsRedirect("/status");
            };

            Get["/SchemaIndex"] = parameters =>
            {
                SyncSourceServiceClient client = GetSourceServiceClient();
                var list = client.GetSchemaEntities(0, 150);

                return View["SchemaIndex", list];
            };

            Get["/ToggleSyncEnabled/{id}"] = parameters =>
            {
                SyncSourceServiceClient client = GetSourceServiceClient();
                var result = client.ToggleSyncEnabled(parameters.id);

                return Response.AsJson(new { newValue = result, success = true });
            };

            Post["/ImportSchema/{id}"] = parameters =>
            {
                SyncSourceServiceClient client = GetSourceServiceClient();
                client.ImportSchema(SyncInstanceId, parameters.id, parameters.type);

                return Response.AsJson(new { newValue = 1, success = true });
            };
        }

        /*private dynamic ImportSchema(dynamic parameters)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            client.ImportSchema(SyncInstanceId, parameters.id, parameters.type);

            return Response.AsJson(new { newValue = 1, success = true });
        }*/

        /*public ViewRenderer DeploySourceEnvironment()
        {
            
        }*/

        private SyncSourceServiceClient GetSourceServiceClient()
        {
            return new SyncSourceServiceClient(SiteSettings.SyncSourceConnInfo);
        }

        private SyncDestinationServiceClient GetDestinationServiceClient()
        {
            return new SyncDestinationServiceClient(SiteSettings.SyncDestinationConnInfo);
        }

    }
}
